package com.pqc.tbk.web.user;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.pqc.tbk.entity.account.User;
import com.pqc.tbk.service.account.UserService;
import com.pqc.tbk.utils.CookiesCRUD;
import com.pqc.tbk.utils.StringUtil;

@Controller
@RequestMapping("/user")
public class UserController {

	private UserService userService;

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		return "/user/login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String fail(
			@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) final String userName,
			final Model model) {
		model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM,
				userName);
		return "/user/login";
	}

	// @RequestMapping("login")
	// public String login(final RedirectAttributes redirectAttributes,
	// final String username, final String password,
	// final HttpServletRequest request, final HttpServletResponse response) {
	// final User user = userService.checkLogin(username, password);
	// if (null == user) {
	// redirectAttributes.addFlashAttribute("msg", "用户名或密码不正确~");
	// return "redirect:/user/_login";
	// } else {
	// // request.getSession().setAttribute("user", user);
	// final String[] name = { "user_nick", "user_username", "user_id" };
	// final String[] value = { user.getNick(), user.getUsername(),
	// user.getId() + "" };
	// CookiesCRUD.addCookie(response, name, value, 60 * 60 * 24 * 7);
	// redirectAttributes.addFlashAttribute("msg", "登录成功~");
	// return "redirect:/";
	// }
	// }

	@RequestMapping("logout")
	public String logout(final RedirectAttributes redirectAttributes,
			final HttpServletResponse response) {
		final String[] name = { "user_id", "user_username", "user_nick" };
		CookiesCRUD.removeCookie(response, name);
		redirectAttributes.addFlashAttribute("msg", "用户已注销~");
		return "redirect:/";
	}

	/**
	 * 
	 * @param v
	 * @param type
	 *            1、验证用户是否存在， 其他、验证邮箱是否存在
	 * @return
	 */
	@RequestMapping(value = "exist", method = RequestMethod.POST)
	@ResponseBody
	public String isExist(@RequestParam("v") final String v,
			@RequestParam("type") final Integer type) {
		if (1 == type) {
			return userService.exist(v) ? "{\"flag\": true}"
					: "{\"flag\": false}";
		} else {
			return userService.existEmail(v) ? "{\"flag\": true}"
					: "{\"flag\": false}";

		}
	}

	@RequestMapping(value = "regist", method = RequestMethod.POST)
	public String regist(final User user, final String inviter,
			final RedirectAttributes redirectAttributes) {

		if (StringUtil.isEmptyOrNull(user.getEmail())
				|| StringUtil.isEmptyOrNull(user.getPassword())) {
			redirectAttributes.addFlashAttribute("msg", "注册失败~ 请填写必填项");
			return "redirect:/user/_regist";
		}

		try {
			userService.regist(user, inviter);
			redirectAttributes.addFlashAttribute("msg", "注册成功~");
			return "redirect:/";
		} catch (final Exception e) {
			redirectAttributes.addFlashAttribute("msg", "注册失败~");
			return "redirect:/user/_regist";
		}
	}

	/**
	 * 有些页面虽然没有经过数据库操作，但还是得由controller转发
	 * 
	 * @param view
	 * @return
	 */
	@RequestMapping("_{view}")
	public String _view(@PathVariable("view") final String view) {
		return "user/" + view;
	}

	@Autowired
	public void setUserService(final UserService userService) {
		this.userService = userService;
	}
}
